package com.pengtu.service.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.QueryFilter.MatchType;
import com.pengtu.dao.app.PlanDao;
import com.pengtu.dao.app.WorkLogDao;
import com.pengtu.dao.app.WorkTaskDao;
import com.pengtu.entity.app.Plan;
import com.pengtu.entity.app.WorkLog;
import com.pengtu.entity.app.WorkTask;

@Component
@Transactional
public class WorkManager {

	private WorkTaskDao workTaskDao;
	private WorkLogDao workLogDao;
	private PlanDao planDao;
	
	@Autowired
	public void setWorkTaskDao(WorkTaskDao workTaskDao) {
		this.workTaskDao = workTaskDao;
	}

	@Autowired
	public void setWorkLogDao(WorkLogDao workLogDao) {
		this.workLogDao = workLogDao;
	}

	@Autowired
	public void setPlanDao(PlanDao planDao) {
		this.planDao = planDao;
	}
	
	//*** 操作分配任务表***//
	public void saveWorkTask(WorkTask workTask){
		workTaskDao.save(workTask);
	}
	
	public void deleteWorkTask(String id){
		workTaskDao.delete(id);
	}
	/**
	 * 
	 * findPageWorkTask:
	 * 适用:模糊查询并按分页获取数据
	 * @param page
	 * @param queryfilter
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly=true)
	public Page<WorkTask> findPageWorkTask(final  Page<WorkTask> page,
			final  QueryFilter queryfilter) {
		// TODO Auto-generated method stub
		return workTaskDao.findPage(page, queryfilter);
	}
	
	@Transactional(readOnly = true)
	public List<WorkTask> getWorktaskList(String username) {
		return workTaskDao.likeBy("executor", username);
	}
	/**
	 * 
	 * findPropertyWorkTask:
	 * 适用:根据属性名称和属性值获得单条数据信息
	 * @param id
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly=true)
	public WorkTask findPropertyWorkTask(String propertyName , String value) {
		// TODO Auto-generated method stub
		if(propertyName.equalsIgnoreCase("id")){
			return workTaskDao.get(value);
		}
		return workTaskDao.findUniqueBy(propertyName, value);
	}
	
	//*** 操作工作日志表***//
	
	public void saveWorkLog(WorkLog workLog){
		workLogDao.save(workLog);
	}
	
	public void deleteWorkLog(String id){
		workLogDao.delete(id);
	}
	@Transactional(readOnly=true)
	public Page<WorkLog> findPageWorkLog(final Page<WorkLog> page,
			final QueryFilter queryfilter) {

		return workLogDao.findPage(page, queryfilter);
	}
	@Transactional(readOnly=true)
	public WorkLog findAppWorkLog(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id")) {
			return workLogDao.get(value);
		}

		return workLogDao.findUniqueBy(propertyName, value);
	}

	//*** 操作我的计划表***//
	
	public void savePlan(Plan plan){
		planDao.save(plan);
	}
	
	public void deletePlan(String id){
		planDao.delete(id);
	}
	
	@Transactional(readOnly = true)
	public Plan getPlan(String id){
		return planDao.get(id);
	}

	/**
	 * 
	 * getPlanList: 模糊查询并按分页获取数据
	 * 适用:
	 * @param page
	 * @param queryFilter
	 * @param username
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	public Page<Plan> getPlanList(Page<Plan> page, QueryFilter queryFilter, String username) {
		queryFilter.addFilter("publisher", username, MatchType.EQ);
		return planDao.findPage(page, queryFilter);
	}
	
	/**
	 * 
	 * getPlans:得到所有的计划
	 * 适用:
	 * @param username
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	public List<Plan> getPlans(String username){
		return planDao.findBy("publisher", username);
	}
	
	/**
	 * 
	 * getTopPlan:得到最新的六条记录
	 * 适用:
	 * @param username
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	public List<Plan> getTopPlan(String username) {
		return planDao.getTopPlan(username);
	}
}


